package com.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice(annotations = {RestController.class, Controller.class})//设置要拦截那些controller的异常
@ResponseBody//返回对象转化为JSON数据
@Slf4j//使用log日志
public class GlobalException {
//    异常处理方法
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)//设置要处理的异常类型
    public R<String> exceptionHandle(SQLIntegrityConstraintViolationException exception){
        log.error(exception.getMessage());
        return R.error("操作失败");
    }

    @ExceptionHandler(CustomException.class)//设置要处理的异常类型
    public R<String> exceptionHandle(CustomException exception){
        log.error(exception.getMessage());
        return R.error(exception.getMessage());
    }
}
